/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ServiceModel;
using System.Net;
using System.ServiceModel.PeerResolvers;



using InterfaceFiles;
using Adiance.VMukti.BAL;
using NetPeerService;

namespace Adiance.VMukti.VCServer
{
    public partial class ServiceHoster : System.Windows.Window
    {
        NetPeerServer serverLoginStat = null;
        NetPeerServer serverPageNav = null;
        NetPeerServer serverPodResize = null;
        NetPeerServer serverUriInfo = null;
        NetPeerServer serverLoginInfo = null;

        ServiceHost fileupload;        
        //ServiceHost logininfo;       

        ZipInfo.CliZipInfoMain c1 = null;
        CliLoginStatus c2 = null;
        CliUriInfo objUri = null;
        CliLoginInfo cliLogin = null;

        NetTcpBinding binding = new NetTcpBinding();

        public ServiceHoster()
        {
            InitializeComponent();

            System.Windows.Application.Current.Exit += new ExitEventHandler(Current_Exit);
            try
            {
                binding.PortSharingEnabled = true;

                fileupload = new ServiceHost(typeof(CsvcFileTransfer));
                fileupload.AddServiceEndpoint(typeof(IsvcFileTransfer), binding, "net.tcp://" + UserInfo.strMachIP + ":2600/FileUpload");
                fileupload.Open();

                serverLoginInfo = new NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/LoginInfo");
                serverLoginInfo.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/LoginInfo");
                serverLoginInfo.OpenServer();
                cliLogin = new CliLoginInfo("net.tcp://" + UserInfo.strMachIP + ":2500/LoginInfo");

                c1 = new ZipInfo.CliZipInfoMain();

                serverUriInfo = new NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/UriInfo");
                serverUriInfo.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/UriInfo");
                serverUriInfo.OpenServer();
                objUri = new CliUriInfo("net.tcp://" + UserInfo.strMachIP + ":2500/UriInfo");

                serverLoginStat = new NetPeerServer("net.tcp://"+UserInfo.strMachIP+":2500/LoginStatus");
                serverLoginStat.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/LoginStatus");                
                serverLoginStat.OpenServer();
                c2 = new CliLoginStatus("net.tcp://" + UserInfo.strMachIP + ":2500/LoginStatus");

                serverPageNav = new NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/PageNavigation");
                serverPageNav.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/PageNavigation");
                serverPageNav.OpenServer();

                serverPodResize = new NetPeerServer("net.tcp://" + UserInfo.strMachIP + ":2500/PodResize");
                serverPodResize.AddEndPoint("net.tcp://" + UserInfo.strMachIP + ":2500/PodResize");
                serverPodResize.OpenServer();

                

            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--ServiceHoster()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                MessageBox.Show("ServiceHoster=" + exp.Message);
            }
        }

        void Current_Exit(object sender, ExitEventArgs e)
        {
            try
            {
                if (fileupload.State != CommunicationState.Closed)
                {
                    fileupload.Close();
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--Current_Exit()1--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //MessageBox.Show(exp.Message);
            }

            //try
            //{
            //    if (logininfo.State != CommunicationState.Closed)
            //    {
            //        logininfo.Close();
            //    }
            //}
            //catch (Exception exp)
            //{
            //    exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--Current_Exit()2--:--");
            //    Adiance.VMukti.BAL.clsUtility.LogException = exp;
            //    //MessageBox.Show(exp.Message);
            //}
            
            try
            {
                serverLoginStat.CloseServer();
                serverPageNav.CloseServer();
                serverPodResize.CloseServer();
                serverUriInfo.CloseServer();
                serverLoginInfo.CloseServer();
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--Current_Exit()3--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //MessageBox.Show("Closing server:"+exp.Message);
            }

            try
            {
                for (int j = 0; j < CliLoginInfo.serverControlslist.Count; j++)
                {
                    try
                    {
                        ((NetPeerServer)CliLoginInfo.serverControlslist[j][0]).CloseServer();
                    }
                    catch (Exception exp)
                    {
                        exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--Current_Exit()4--:--");
                        Adiance.VMukti.BAL.clsUtility.LogException = exp;
                        //MessageBox.Show("Closing Control's Service:" + exp.Message + "for" + svcLoginInfo.serverControlslist[j].ToString());
                    }
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--Current_Exit()5--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //MessageBox.Show("Adiance.VMukti.VCServer--:--ServiceHoster.xaml.cs--:--Current_Exit()5--:--"+exp.Message);
            }
        }
    }
}